package com.example.testapplication;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

/**
 * @author wwq on 2019/7/17  15:07.
 */
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public class HeaderViewHolder extends RecyclerView.ViewHolder{
        HeaderViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }

    public  class ViewHolder extends RecyclerView.ViewHolder{
        TextView title;
        ViewHolder(View v) {
            super(v);
            title =v.findViewById(R.id.title);
        }
    }

    private List<Question> questionList;

    RecyclerViewAdapter(List<Question> list){
        this.questionList = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 111){
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.header_view,parent,false);
            return new HeaderViewHolder(view);
        }
        View view =  LayoutInflater.from(parent.getContext()).inflate(R.layout.question_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == 111){
            return;
        }
        Question question = questionList.get(position-1);
        ((ViewHolder)holder).title.setText(question.getTitle());

    }

    @Override
    public int getItemCount() {
        return questionList.size()+1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0){
            return 111;
        }
        return super.getItemViewType(position);
    }
}
